package com.dolceplan.dolceapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.AsyncTask;
import android.util.Log;

public class UpdateOffersTask extends AsyncTask<Void, Void, String> {

	@Override
	protected String doInBackground(Void... params) {
		String dolceUrl = "http://www.osvid.org/DolceServer/getOffer_2.php";
    	
		/* Get lat and lon to query database
		//String lat = Double.toString(location.getLatitude());
    	//String lon = Double.toString(location.getLongitude());
		//ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        //nameValuePairs.add(new BasicNameValuePair("lat", lat));
        //nameValuePairs.add(new BasicNameValuePair("lon", lon))
		*/
		
    	String resultSQL = "";
       	InputStream is = null;		
                
        // Http post
        try {
        	HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(dolceUrl);
            //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
        	Log.e("log_tag", "Error in http connection "+e.toString());
        }   	
    	        
        // Convert response to string
        try {
        	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
            	sb.append(line + "\n");
            }
            is.close();
            resultSQL=sb.toString();
            Log.i("info", resultSQL);
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result "+e.toString());
        }
        
		return resultSQL;
	}
}
